Skip to content

feat: Implement Bitwise and Shift Operator Support #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 25, 2023

Conversation

ckganesan
Copy link
Contributor

@ckganesan ckganesan commented Dec 11, 2023

This PR introduces support for bitwise and shift operators. With this enhancement, users can now perform advanced bitwise operations, including AND, OR, XOR, and NOT, as well as left and right shift operations.

&    bitwise AND            integers
// To manage bitwise OR operations and various callbacks (predicates, built-ins, and custom functions)
|    bitwise OR             integers
// Duplicate the Caret operator to prevent confusion with the Exponent operator.
^^    bitwise XOR           integers
&^   bit clear (AND NOT)    integers

<<   left shift             integer << integer >= 0
>>   right shift            integer >> integer >= 0

Ref: #207

@antonmedv
Copy link
Member

Let’s not override pipe operator. This will create confusion. Also ^^ is really odd.

What about using full names as operators for bitwise ops?

I already know a good one:

  • xor

    A xor B

We just need to find nice ones for other. I know some languages do that.

@antonmedv
Copy link
Member

And let’s implement them as functions. So we don’t need to worry about precedence.

@antonmedv
Copy link
Member

Or as alternative solution add a special "macro": bit which will be converting those expressions:

bit(A | B ^ ~C) 

@ckganesan
Copy link
Contributor Author

And let’s implement them as functions. So we don’t need to worry about precedence.

Following the comments, I will also be adding the following built-in functions, along with their respective precedences.

// Bitwise ops
&, bitand     -    bitwise AND            integers
bor, bitor    -    bitwise OR             integers
xor, bitxor   -    bitwise XOR            integers
&^, bitnand   -    bit clear (AND NOT)    integers
~, bitnot     -    bit NOT                integer

// Bit Shift ops
<<, bitshl    -    left shift             integer << integer >= 0
>>, bitshr    -    right shift            integer >> integer >= 0
>>>, bitushr  -    unsigned right shift 

@antonmedv
Copy link
Member

But let's drop operators

@antonmedv antonmedv merged commit 9b8d741 into expr-lang:master Dec 25, 2023
@ckganesan ckganesan deleted the support-bitwise-shift-operation branch December 26, 2023 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants